//
//  CigarettelLibaryViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/25.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "CigarettelLibaryViewController.h"
#import "TAMacro.h"
#import "BaseCollectionView.h"
#import "LibraryCollectionViewCell.h"
#import "BBMenu.h"
#import "HWLNetworkTool.h"
#import "UIImageView+WebCache.h"
#import "Cigarettes.h"
#import "SelectViewController.h"
#import "SearchCigaretteTableViewController.h"
#import "ProductDetailTableViewController.h"
#import "MJRefresh.h"
@interface CigarettelLibaryViewController ()<
        SecondViewControllerDelegate,
        UICollectionViewDelegate,
        UICollectionViewDataSource,
        UICollectionViewDelegateFlowLayout,
        BBSuperMenuDataSource,
        BBSuperMenuDelegate


>



@property (nonatomic,retain)BaseCollectionView *collectionView;
@property (nonatomic, strong) BBMenu *menu;
@property (nonatomic, strong) NSArray *classify;
@property (nonatomic, strong) NSArray *arrayOfPrice;
@property (nonatomic, strong) NSArray *arrayOfCatrame;
@property (nonatomic, strong) NSArray *arrayOfScore;
@property (nonatomic, strong) NSArray *arrayOfDefault;
@property (nonatomic, strong) NSArray *arrayOfclassify2;
@property (nonatomic, strong) NSArray *arrayOfALLBrand;
@property (nonatomic, strong) NSArray *arrayOfALLBrandNo;
@property (nonatomic, strong) NSMutableArray *arrayOfLandBrand;
@property (nonatomic, strong) NSMutableArray *arrayOfLandBrandNo;
@property (nonatomic, strong) NSMutableArray *arrayOfForeignBrand;
@property (nonatomic, strong) NSMutableArray *arrayOfForeignBrandNo;
@property (nonatomic, strong) NSMutableArray *arrayOfHMTBrand;
@property (nonatomic, strong) NSMutableArray *arrayOfHMTBrandNo;
@property (nonatomic, strong) NSMutableArray *arrayOfHistory;
@property (nonatomic, strong) NSMutableArray *arrayOfHistoryNo;
@property (nonatomic, strong) NSMutableArray *arrayOfProduct;
@property (nonatomic, strong) NSMutableArray *arrayOfNewSearch;

@property (nonatomic, assign) NSInteger order; //排列规则
@property (nonatomic, assign) NSInteger ordertype;//排列顺序
@property (nonatomic, copy)   NSString *brand;  //品牌序号
@property (nonatomic, copy) NSString *price_max;
@property (nonatomic, copy) NSString *price_min;
@property (nonatomic, copy) NSString *tar_content; //焦油量
@property (nonatomic, copy) NSString *co_content;//一氧化碳量
@property (nonatomic, copy) NSString *nicotine_content;//烟碱量
@property (nonatomic, assign) NSInteger type;// 1烤烟型 2混合型 3外香型 4雪茄型 5雪茄烟


@end


@implementation CigarettelLibaryViewController


- (void)viewWillAppear:(BOOL)animated{

     self.tabBarController.tabBar.hidden = NO;
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.title = @"烟库";

}





- (void)viewDidLoad{
   
    
    [self handleData];
    [self createNavigation];
    [self createCollectionView];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self Data];
    }];

}


- (void)handleData{

    // 数据
   
    self.order = 0;
    self.ordertype = 0;
    self.type = 0;
    self.classify = @[@"默认排序",@"按价格", @"按焦油", @"按评分"];
    self.arrayOfPrice = @[@"按价格从低到高",@"按价格从高到低"];
    self.arrayOfCatrame = @[@"按焦油从低到高",@"按焦油从高到低"];
    self.arrayOfScore = @[@"按评分由低到高",@"按评分由高到低"];
    self.arrayOfDefault = @[];
    self.arrayOfclassify2 = @[@"全部品牌",@"大陆品牌",@"国外品牌",@"港澳台品牌",@"历史品牌"];
    self.arrayOfALLBrand = @[];
    self.arrayOfLandBrand = [NSMutableArray array];
    self.arrayOfForeignBrand = [NSMutableArray array];
    self.arrayOfHMTBrand = [NSMutableArray array];
    self.arrayOfHistory = [NSMutableArray array];
    self.arrayOfLandBrandNo = [NSMutableArray array];
    self.arrayOfForeignBrandNo = [NSMutableArray array];
    self.arrayOfHMTBrandNo = [NSMutableArray array];
    self.arrayOfHistoryNo = [NSMutableArray array];
    self.arrayOfNewSearch = [NSMutableArray array];
    
    
    _menu = [[BBMenu alloc]initWithOrigin:CGPointMake(0, 64) andHeight:64];
    _menu.delegate = self;
    _menu.dataSource = self;
    [self.view addSubview:_menu];
    [self Data];

}

- (NSInteger)numberOfColumnsInMenu:(BBMenu *)menu {
    return 3;
}

- (NSInteger)menu:(BBMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    if (column == 0) {
        return self.classify.count;
    }else if(column == 1) {
        return self.arrayOfclassify2.count;
    }else if (column == 2){
        return 1;
    }
    return 0;
}

- (NSString *)menu:(BBMenu *)menu titleForRowAtIndexPath:(BBIndexPath *)indexPath {
    if (indexPath.column == 0) {
        return self.classify[indexPath.row];
    }else if(indexPath.column == 1) {
        return self.arrayOfclassify2[indexPath.row];
    }else if (indexPath.column == 2){
        return @"筛选器";
    }
    
    return 0;
}

//用来显示没一列数量的

//- (NSString *)menu:(BBMenu *)menu imageNameForRowAtIndexPath:(BBIndexPath *)indexPath {
//    if (indexPath.column == 0 || indexPath.column == 1) {
//        return @"baidu";
//    }
//    return nil;
//}
//
//- (NSString *)menu:(BBMenu *)menu imageForItemsInRowAtIndexPath:(BBIndexPath *)indexPath {
//    if (indexPath.column == 0 && indexPath.item >= 0) {
//        return @"baidu";
//    }
//    return nil;
//}

- (NSString *)menu:(BBMenu *)menu detailTextForRowAtIndexPath:(BBIndexPath *)indexPath {
    
    return nil;
}

- (NSString *)menu:(BBMenu *)menu detailTextForItemsInRowAtIndexPath:(BBIndexPath *)indexPath {
    return nil;
}

- (NSInteger)menu:(BBMenu *)menu numberOfItemsInRow:(NSInteger)row inColumn:(NSInteger)column {
    if (column == 0) {
        if (row == 0) {
            return self.arrayOfDefault.count;
        }else if (row == 1) {
            return self.arrayOfPrice.count;
        }else if (row == 2) {
            return self.arrayOfCatrame.count;
        }else if (row == 3) {
            return self.arrayOfScore.count;
        }
    }
    if (column == 1) {
        if (row == 0) {
            return self.arrayOfALLBrand.count;
        }else if (row == 1) {
            return self.arrayOfLandBrand.count;
        }else if (row == 2) {
            return self.arrayOfForeignBrand.count;
        }else if (row == 3) {
            return self.arrayOfHMTBrand.count;
        }else if (row == 4) {
            return self.arrayOfHistory.count;

        }
    }
    return 0;
}

- (NSString *)menu:(BBMenu *)menu titleForItemsInRowAtIndexPath:(BBIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    if (indexPath.column == 0) {
        if (row == 0) {
            return self.arrayOfDefault[indexPath.item];
        }else if (row == 1) {
            return self.arrayOfPrice[indexPath.item];
        }else if (row == 2) {
            return self.arrayOfCatrame[indexPath.item];
        }else if (row == 3) {
            return self.arrayOfScore[indexPath.item];
        }
    }
    
    if (indexPath.column == 1) {
        if (row == 0) {
            return self.arrayOfALLBrand[indexPath.item];
        }else if (row == 1) {
            return self.arrayOfLandBrand[indexPath.item];
        }else if (row == 2) {
            return self.arrayOfForeignBrand[indexPath.item];
        }else if (row == 3) {
            return self.arrayOfHMTBrand[indexPath.item];
        }else if (row == 4) {
            return self.arrayOfHistory[indexPath.item];
        }
    }
    
    return nil;
}




- (void)createNavigation{
    
    //设置navigation相关属性
  
    
    /*34 47 60*/
    
   
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"  查找" style:UIBarButtonItemStylePlain target:self action:@selector(search)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空筛选器" style:UIBarButtonItemStylePlain target:self action:@selector(clear)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
}

- (void)search{

    SearchCigaretteTableViewController *searchController = [[SearchCigaretteTableViewController alloc]init];
    
    [self.navigationController pushViewController:searchController animated:YES];

}

- (void)clear{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"筛选条件已清空" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    self.tar_content = nil;
    self.co_content = nil;
    self.type = 0;
    self.nicotine_content = nil;
    self.price_max = nil;
    self.price_min = nil;
    [self searchData];
    
    [self presentViewController:alert animated:YES completion:nil];



}

- (void)createCollectionView{
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //*设置item大小/
    layout.itemSize = CGSizeMake((SCREEN_SIZE_WIDTH_ME / 2) - 15 , (SCREEN_SIZE_WIDTH_ME / 2) *1.2);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
   
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0 , 128, SCREEN_SIZE_WIDTH_ME , (SCREEN_SIZE_HEIGTH_ME) - 49) collectionViewLayout:layout];
   
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = NO;
    
    
    
   // self.collectionView.backgroundColor = [UIColor colorWithRed:249 green:249 blue:249 alpha:1];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[LibraryCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    self.automaticallyAdjustsScrollViewInsets = NO;

    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.arrayOfProduct) {
        
         return self.arrayOfProduct.count;
    }
    
    return self.arrayOfNewSearch.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (self.arrayOfProduct) {
        LibraryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"forIndexPath:indexPath];
        
        
        NSDictionary *dic = self.arrayOfProduct[indexPath.row];
        NSURL *url = [NSURL URLWithString:dic[@"photo"]];
        [cell.imageViewLift sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.name.text = dic[@"name"];
        cell.catrameinput.text = dic[@"catrame"];
        cell.priceinput.text = dic[@"price"];
        
        return cell;

    }else if (self.arrayOfNewSearch){
    
        LibraryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"forIndexPath:indexPath];
        NSDictionary *dic = self.arrayOfNewSearch[indexPath.row];
        NSURL *url = [NSURL URLWithString:dic[@"photo"]];
        [cell.imageViewLift sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.name.text = dic[@"name"];
        cell.catrameinput.text = dic[@"catrame"];
        cell.priceinput.text = dic[@"price"];
        
        return cell;

    }
    
    return nil;
}
- (void)menu:(BBMenu *)menu didSelectRowAtIndexPath:(BBIndexPath *)indexPath {
   
  
    
   
    if (indexPath.item >= 0 ){
        
        self.order = indexPath.row;
        
        if (indexPath.column == 0) {
            
            self.order = indexPath.row;
            self.ordertype = indexPath.item;
            
        }else if(indexPath.column == 1)
        {
            self.order = indexPath.row;
            
        if (indexPath.row == 1 && indexPath.item != -1) {
            self.brand = self.arrayOfLandBrandNo[indexPath.item];
            
        }if (indexPath.row == 2 &&indexPath.item != -1) {
            self.brand = self.arrayOfForeignBrandNo[indexPath.item];
            
        }if (indexPath.row == 3 &&indexPath.item != -1) {
            self.brand = self.arrayOfHMTBrandNo[indexPath.item];
            
        }if (indexPath.row == 4 &&indexPath.item != -1) {
            self.brand = self.arrayOfHistoryNo[indexPath.item];
            
        }
        }
    
    }
    if (indexPath.column == 2) {
      //  NSLog(@"推出筛选器");
        
        SelectViewController *selectViewController = [[SelectViewController alloc]init];
        selectViewController.secdongDelegate = self;
        
        [self.navigationController pushViewController:selectViewController animated:YES];
     
    }

    
 [self searchData];
}
 
   
-(void)changeValueMAX:(NSString *)price_max min:(NSString *)price_min tar:(NSString *)tar_content co:(NSString *)co_content nio:(NSString *)nicotine_content type:(NSInteger)type{


    self.price_min = price_min;
    self.price_max = price_max;
    self.tar_content = tar_content;
    self.co_content = co_content;
    self.nicotine_content = nicotine_content;
    self.type = type;
    

    [self searchData];

}


- (void)searchData{
   
   
    
    NSString *order = [NSString stringWithFormat:@"%lu",self.order];
    NSString *ordertype = [NSString stringWithFormat:@"%lu",self.ordertype];
    NSString *type = [NSString stringWithFormat:@"%lu",self.type];
    NSString *strOfBody = [[[@"ln=50&order=" stringByAppendingString:order]stringByAppendingString:@"&ordertype="]stringByAppendingString:ordertype];
    if (self.brand == nil) {
       
    }else{
        strOfBody = [[strOfBody stringByAppendingString:@"&brand="]stringByAppendingString:self.brand];
         }
    
    if (self.price_min.length > 0 ) {
    
      strOfBody = [[strOfBody stringByAppendingString:@"&price_min="]stringByAppendingString:self.price_min];
    }
    
    if (self.price_max.length > 0) {
        
        
        strOfBody = [[strOfBody stringByAppendingString:@"&price_max="]stringByAppendingString:self.price_max];
    }
    
    
    if (self.co_content.length > 0) {
     strOfBody = [[strOfBody stringByAppendingString:@"&co_content="]stringByAppendingString:self.co_content];
    }
    if (self.tar_content.length > 0) {
        strOfBody = [[strOfBody stringByAppendingString:@"&tar_content="]stringByAppendingString:self.tar_content];

    
    }
    
    if (self.nicotine_content.length > 0) {
     
        strOfBody = [[strOfBody stringByAppendingString:@"&nicotine_content="]stringByAppendingString:self.nicotine_content];
    }
    
   if (type.length > 0) {
     
       strOfBody = [[strOfBody stringByAppendingString:@"&type="]stringByAppendingString:type];
       
    }
  
    
      NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
      [HWLNetworkTool POST:@"http://app.yanyue.cn/sort/search" andBody:strOfBody andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
          
            self.arrayOfProduct = nil;
            self.arrayOfNewSearch = result[@"cigarettes"];
            [self.collectionView reloadData];
          
         
            
        } andFailureBlock:^(NSError *error) {
            
        }];
        
    
    }

- (void)Data{
    
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    
    [HWLNetworkTool POST:@"http://app.yanyue.cn/sort/sort" andBody:@"openudid=5ae325c9ec" andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        if (result){
            
            for (NSDictionary *dic in result[@"0"][@"csort"]) {
                NSString *title = dic[@"title"];
                NSString *sortid = dic[@"sortid"];
                [self.arrayOfLandBrand addObject:title];
                [self.arrayOfLandBrandNo addObject:sortid];
            }
            for (NSDictionary *dic in result[@"1"][@"csort"]) {
                NSString *title = dic[@"title"];
                NSString *sortid = dic[@"sortid"];
                [self.arrayOfForeignBrand addObject:title];
                [self.arrayOfForeignBrandNo addObject:sortid];
            }
            for (NSDictionary *dic in result[@"2"][@"csort"]) {
                NSString *title = dic[@"title"];
                NSString *sortid = dic[@"sortid"];
                [self.arrayOfHMTBrand addObject:title];
                [self.arrayOfHMTBrandNo addObject:sortid];
            }
            for (NSDictionary *dic in result[@"3"][@"csort"]) {
                
                NSString *title = dic[@"title"];
                NSString *sortid = dic[@"sortid"];
                [self.arrayOfHistory addObject:title];
                [self.arrayOfHistoryNo addObject:sortid];
            }
            self.arrayOfProduct = result[@"product"];
        }
      
        [self.collectionView reloadData];
        
        
        [self.collectionView.mj_header endRefreshing];
        
    } andFailureBlock:^(NSError *error) {
        
    }];}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    ProductDetailTableViewController *productDetail = [[ProductDetailTableViewController alloc]init];
    
    if (self.arrayOfProduct) {
        
        
        
         productDetail.productId = self.arrayOfProduct[indexPath.row][@"id"];
         [self.navigationController pushViewController:productDetail animated:YES];
    }
    
    else if (self.arrayOfNewSearch){
   
      productDetail.productId = self.arrayOfNewSearch[indexPath.row][@"id"];
        [self.navigationController pushViewController:productDetail animated:YES];
        
}

   
    
}

@end
